package com.learn;

import java.io.File;

import org.apache.catalina.Context;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.valves.AccessLogValve;
import org.apache.coyote.http11.Http11NioProtocol;
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatContextCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.AbstractServletWebServerFactory;
import org.springframework.stereotype.Component;

/**
 * 第一种定制tomcat的方式
 * @author Romanceling
 *
 */
//@Component
public class MyWebServerFactoryCustomizer implements WebServerFactoryCustomizer<AbstractServletWebServerFactory>{

	@Override
	public void customize(AbstractServletWebServerFactory factory) {
		TomcatServletWebServerFactory tomcat = (TomcatServletWebServerFactory)factory;
		tomcat.setPort(8888);
		tomcat.setBaseDirectory(new File("E:/myspringbootlogs/tomcat"));

		//access日志
		tomcat.addContextValves(createAccessLogValve());
		
		//tomcat连接数
		tomcat.addConnectorCustomizers(new MyTomcatContextCustomizer());
	}

	private AccessLogValve createAccessLogValve(){
		AccessLogValve log = new AccessLogValve();
		log.setEnabled(true);
		log.setDirectory("E:/myspringbootlogs/tomcatlog");
		log.setPattern(org.apache.catalina.valves.Constants.AccessLog.COMMON_PATTERN);
		log.setPrefix("tomcat-accesslog");
		log.setSuffix(".tomcatlog");
		
		
		return log;
	}
	
	private class MyTomcatContextCustomizer implements TomcatConnectorCustomizer{

		@Override
		public void customize(Connector connector) {
			Http11NioProtocol protocol = (Http11NioProtocol)connector.getProtocolHandler();
			//2000个连接
			protocol.setMaxConnections(2000);
			//500个线程
			protocol.setMaxThreads(500);
		}
	}
}
